package com.net.xpay.common.validator;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.xpay.common.domain.CheckCode;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.domain.partner.Partner;
import com.net.xpay.common.enums.partner.PartnerLevel;
import com.net.xpay.common.enums.user.CheckCodeType;
import com.net.xpay.common.enums.user.CommissionMode;
import com.net.xpay.common.enums.user.ServicePayMode;
import com.net.xpay.common.manager.CheckCodeManager;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.common.model.PasswordModel;
import com.net.xpay.common.model.RegisterModel;
import com.net.xpay.common.model.UpdateUserServicePriceModel;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.common.exception.BusinessException;
import com.net.common.util.PhoneUtil;
import com.net.common.util.SerializationUtil;
import com.net.xpay.common.manager.partner.PartnerManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * @author  on 27/01/2018.
 */
@Slf4j
@Component
public class UserValidator {
    private static final int MAX_PASS_LENGTH = 20; //最大的密码长度
    private static final int MIN_PASS_LENGTH = 6; //最小的密码长度

    @Autowired
    private CheckCodeManager checkCodeManager;
    @Autowired
    private CheckCodeValidator checkCodeValidator;
    @Autowired
    private UserManager userManager;
    @Autowired
    private PartnerManager partnerManager;

    public void validateRegisterInfo(RegisterModel registerModel, boolean isAutoRegister) {
        //验证手机号
        if (!PhoneUtil.isEffectiveMobile(registerModel.getMobile())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号格式不正确");
        }

        if (isAutoRegister) {
            return;
        }

        //验证手机验证码
        CheckCode checkCode = checkCodeManager.findByMobileAndType(registerModel.getMobile(), CheckCodeType.REGISTER);
        checkCodeValidator.validateCheckCode(checkCode, registerModel.getCheckCode());

        //验证密码
        validatePassword(registerModel.getPassword(), registerModel.getRepeatPassword());

        //验证协议
        if (registerModel.getAcceptProtocol() == null || !registerModel.getAcceptProtocol()) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请同意用户协议");
        }
    }

    public void validatePassword(PasswordModel passwordModel, CheckCodeType checkCodeType) {
        //验证手机号
        if (!PhoneUtil.isEffectiveMobile(passwordModel.getMobile())) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "手机号格式不正确");
        }

        //验证手机验证码
        CheckCode checkCode = checkCodeManager.findByMobileAndType(passwordModel.getMobile(), checkCodeType);
        checkCodeValidator.validateCheckCode(checkCode, passwordModel.getCheckCode());

        //验证密码
        validatePassword(passwordModel.getPassword(), passwordModel.getRepeatPassword());

    }

    private void validatePassword(String password, String repeatPassword) {
        if (Strings.isNullOrEmpty(password) || Strings.isNullOrEmpty(repeatPassword)) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "密码不能为空");
        }
        if (!Objects.equals(password, repeatPassword)) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "两次输入的密码必须相等");
        }
        if (password.length() > MAX_PASS_LENGTH || password.length() < MIN_PASS_LENGTH) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "密码长度必须在6到20个字符");
        }
    }

    public void validateUpdateServicePrice(UpdateUserServicePriceModel model) {
        log.info("validateUpdateServicePrice model={}", SerializationUtil.obj2String(model));
        Preconditions.checkArgument(model != null && model.getId() != null);
        User user = userManager.getById(model.getId());

        if (user.getPartnerId() != null) {
            Partner partner = partnerManager.getById(user.getPartnerId());
            if (partner.getLevel() == PartnerLevel.LEVEL_2) {
                //合伙人的上级id必须保证和用户上的上级id一致
                if (!partner.getFatherPartnerId().equals(user.getFatherPartnerId())) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "商户合伙人设置异常");
                }
            } else {
                if (user.getFatherPartnerId() != null) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "商户合伙人应不存在父级合伙人");
                }
            }
        }

        //公共
        if (model.getMode() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请选择收费模式");
        }
        if (model.getMode() == ServicePayMode.FIXED_AMOUNT) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "只能选择按转账金额*费率收费");
        }
        if (model.getServiceRatio() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请输入到户费率");
        }
        if (model.getBankServiceRatio() == null) {
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请输入到卡费率");
        }
//        if (model.getServiceRatio().compareTo(new BigDecimal("0.000")) < 0) {
//            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到户费率不能小于0.000");
//        }
//        if (model.getServiceRatio().compareTo(new BigDecimal("0.01")) >= 0) {
//            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到户费率不能大于等于0.01");
//        }
//        if (model.getBankServiceRatio().compareTo(new BigDecimal("0.000")) < 0) {
//            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到卡费率不能小于0.000");
//        }
//        if (model.getBankServiceRatio().compareTo(new BigDecimal("0.01")) >= 0) {
//            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到卡费率不能大于等于0.01");
//        }

        //有合伙人
        if (user.getPartnerId() != null) {
            if (model.getCommissionMode() == null) {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请选择分佣模式");
            }

            if (model.getCommissionMode() == CommissionMode.FIXED_MODE || model.getCommissionMode() == CommissionMode.STAT_MODE) {
                if (model.getBenefitRatio() == null) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "合伙人分佣比例不能为空");
                }
//                if (model.getBenefitRatio().compareTo(new BigDecimal("0.8")) > 0) {
//                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "合伙人分佣比例不能大于0.8");
//                }
//                if (model.getBenefitRatio().compareTo(new BigDecimal("0")) < 0) {
//                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "合伙人分佣比例不能小于于0");
//                }
            } else if (model.getCommissionMode() == CommissionMode.COST_MODE) {
                //到户成本
                if (model.getCostServiceRatio() == null) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到户成本不能为空");
                }

//                if (model.getCostServiceRatio().compareTo(new BigDecimal("0.001")) < 0) {
//                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到户成本不能低于0.001");
//                }

//                if (model.getCostServiceRatio().compareTo(new BigDecimal("1")) >= 0) {
//                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到户成本不能高于1");
//                }
//
//                if (model.getCostServiceRatio().compareTo(model.getServiceRatio()) >= 0) {
//                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到户成本必须小于到户费率");
//                }

                //到卡成本
                if (model.getBankCostServiceRatio() == null) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到卡成本不能为空");
                }

//                if (model.getBankCostServiceRatio().compareTo(new BigDecimal("0.001")) < 0) {
//                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到卡成本不能低于0.001");
//                }
//
//                if (model.getBankCostServiceRatio().compareTo(new BigDecimal("1")) >= 0) {
//                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到卡成本不能高于1");
//                }
//                if (model.getBankCostServiceRatio().compareTo(model.getBankServiceRatio()) >= 0) {
//                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到卡成本必须小于到卡费率");
//                }
            } else {
                throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "分佣方式错误");
            }


            if (user.getFatherPartnerId() != null) {
                //二级合伙人
                if (model.getFatherCommissionMode() == null) {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请选择上级模式");
                }
                if (model.getCommissionMode() == CommissionMode.FIXED_MODE) {
                    if (model.getFatherCommissionMode() != CommissionMode.FIXED_MODE) {
                        throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "上级模式选择错误");
                    }
                    if (model.getFatherBenefitRatio() == null) {
                        throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "上级合伙人分佣比例不能为空");
                    }
//                    if (model.getFatherBenefitRatio().compareTo(new BigDecimal("0.8")) > 0) {
//                        throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "上级合伙人分佣比例不能大于0.8");
//                    }
//                    if (model.getFatherBenefitRatio().compareTo(new BigDecimal("0")) < 0) {
//                        throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "上级合伙人分佣比例不能小于0");
//                    }
                } else if (model.getCommissionMode() == CommissionMode.COST_MODE){
                    if (model.getFatherCommissionMode() == CommissionMode.FIXED_MODE) {
                        if (model.getFatherBenefitRatio() == null) {
                            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "请输入到户上级合伙人分佣费率");
                        }
//                        if (model.getFatherBenefitRatio().compareTo(new BigDecimal("0.8")) > 0) {
//                            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到户上级合伙人分佣费率不能大于0.8");
//                        }
//                        if (model.getFatherBenefitRatio().compareTo(new BigDecimal("0")) < 0) {
//                            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "上级合伙人分佣比例不能小于0");
//                        }
                    } else if (model.getFatherCommissionMode() == CommissionMode.COST_MODE) {
                        if (model.getFatherServiceRatio() == null) {
                            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到支付宝上级合伙人分佣费率不能为空");
                        }
//                        if (model.getCostServiceRatio().subtract(model.getFatherServiceRatio()).compareTo(new BigDecimal("0.001")) < 0) {
//                            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到户成本-到户上级合伙人分佣费率必须大于等于0.001");
//                        }
                        if (model.getFatherBankServiceRatio() == null) {
                            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到卡上级合伙人分佣费率不能为空");
                        }
//                        if (model.getBankCostServiceRatio().subtract(model.getFatherBankServiceRatio()).compareTo(new BigDecimal("0.001")) < 0) {
//                            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "到卡成本-到卡上级合伙人分佣费率必须大于等于0.001");
//                        }
                    }
                } else {
                    throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "分佣方式错误");
                }
            }
        }

    }
}
